package com.gome.axwl.aoc.standalone.config.mq;

import com.cloudzone.cloudmq.api.open.base.MsgListener;
import com.cloudzone.cloudmq.api.open.bean.ConsumerBean;
import com.cloudzone.cloudmq.api.open.bean.Subscription;
import com.gome.axwl.aoc.web.controller.mq.consumer.MsgConsumerListener;
import com.gome.axwl.aoc.web.controller.mq.producer.ProducerBeanImExt;
import com.gome.commons.serialization.SerializationService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author John Li
 * @date 2018/5/3 16:11
 * @opyright(c) gome inc Gome Co.,LTD
 */
@Configuration
@PropertySource("classpath:gmq.properties")
public class MqBeanConfig implements BeanNameAware, BeanFactoryAware {

    private static Logger logger = LoggerFactory.getLogger(MsgConsumerListener.class);

    @Autowired
    SerializationService serializationService;

    @Autowired
    MsgConsumerListener msgConsumerListener;

    @Value("${gomePubGroup}")
    private String gomePubGroup;

    @Value("${gomePubTopic}")
    private String gomePubTopic;

    @Value("${mqTopic_Auth}")
    private String mqTopicAuth;

    @Value("${gomeAxwlAocDriverIntegrationTag}")
    private String gomeAxwlAocDriverIntegrationTag;

    @Bean
    MsgConsumerListener msgConsumerListener() {
        logger.info("[loading] msgConsumerListener enter before");
        MsgConsumerListener msgConsumerListener = new MsgConsumerListener();
        logger.info("[loading] msgConsumerListener enter after");
        return msgConsumerListener;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    ConsumerBean msgConsumer() {
        ConsumerBean msgConsumer = new ConsumerBean();
        logger.info("[loading] msgConsumer enter");
        //属性
        Properties properties = new Properties();
        properties.setProperty("ConsumerGroupId", gomePubGroup);
        properties.setProperty("TOPIC_NAME_AND_AUTH_KEY", mqTopicAuth);
        msgConsumer.setProperties(properties);

        Map<Subscription, MsgListener> subscriptionTable = new HashMap<Subscription, MsgListener>();
        Subscription subscription = new Subscription();
        subscription.setTopic(gomePubTopic);
        StringBuffer tagExp = new StringBuffer();
        if (!StringUtils.isEmpty(gomeAxwlAocDriverIntegrationTag)) {
            tagExp.append(gomeAxwlAocDriverIntegrationTag);
        }

        if (tagExp.length() > 0) {
            subscription.setExpression(tagExp.toString());
        }
        logger.info("[loading] msgConsumer enter when msgConsumerListener is:{}", msgConsumerListener == null ? "null" : "exist");
        subscriptionTable.put(subscription, msgConsumerListener);

        msgConsumer.setSubscriptionTable(subscriptionTable);

        return msgConsumer;
    }

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    ProducerBeanImExt imProducer() {
        ProducerBeanImExt imProducer = new ProducerBeanImExt();

        //属性
        Properties properties = new Properties();
        properties.setProperty("ProducerGroupId", gomePubGroup);
        properties.setProperty("TOPIC_NAME_AND_AUTH_KEY", mqTopicAuth);

        imProducer.setProperties(properties);
        imProducer.setTopic(gomePubTopic);
        imProducer.setSerializationService(serializationService);
        return imProducer;
    }


    @Override
    public void setBeanName(String name) {

    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

    }
}
